//
//  KGPageMenuView.swift
//  viewcontroller_test
//
//  Created by 潘茹燕 on 16/4/12.
//  Copyright © 2016年 海南开工信息技术有限公司. All rights reserved.
//

import UIKit

class KGPageMenuItemView : UILabel {
    
}


class KGPageMenuView: UIScrollView {

    
    
    var useSameWidth = false
    var widthOfItem = CGFloat(80.0)
    
    private var selectionIndicatorView: UIView?
    
    var selectionIndicatorHeight = CGFloat(2.0)
    
    var Font = UIFont.systemFontOfSize(13)
    
    var itemEdgeInsets = UIEdgeInsets(top: 8.0, left: 8.0, bottom: 8.0, right: 8.0)
    
    
    private var fitSize = CGSizeZero
    
    private var selectedView: UIView?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        showsVerticalScrollIndicator = false
        showsHorizontalScrollIndicator = false
        
        
        selectionIndicatorView = UIView(frame: CGRect(x: 0, y: frame.height  - selectionIndicatorHeight, width: 80, height: selectionIndicatorHeight))
        selectionIndicatorView!.backgroundColor = UIColor.orangeColor()
        addSubview(selectionIndicatorView!)
    }
    
    init(pages:[String], frame: CGRect) {
        
        super.init(frame: frame)
        
        for (index, page) in pages.enumerate() {
            
            
            
            let itemView = UILabel()
            itemView.text = page
            itemView.userInteractionEnabled = true
            itemView.textAlignment = .Center
            itemView.font = Font
            
            //itemView.backgroundColor = (index % 2 == 1) ? UIColor.blueColor() : UIColor.greenColor()
            
            
            if index == 0 {
                selectedView = itemView
            }
            
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(KGPageMenuView.tapGestureRecognizer(_:)))
            itemView.addGestureRecognizer(tapGesture)

            addSubview(itemView)
            
        }
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setPages(pages: [String]) {
        
        for view in subviews {
            
            if view is UILabel {
                view.removeFromSuperview()
            }
        }
        
        for (index, page) in pages.enumerate() {
            
            
            
            let itemView = UILabel()
            itemView.text = page
            itemView.userInteractionEnabled = true
            itemView.textAlignment = .Center
            itemView.font = Font
            
            //itemView.backgroundColor = (index % 2 == 1) ? UIColor.blueColor() : UIColor.greenColor()
            
            
            if index == 0 {
                selectedView = itemView
            }
            
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(KGPageMenuView.tapGestureRecognizer(_:)))
            itemView.addGestureRecognizer(tapGesture)
            
            addSubview(itemView)
            
        }
        
        layoutSubviews()
        
    }
    
    
    func tapGestureRecognizer(sender: AnyObject) {
        
        if let gestureRecognizer = sender as? UITapGestureRecognizer {
            //NSLog("\((gestureRecognizer.view as? UILabel)?.text)")
            
            UIView.animateWithDuration(0.2, animations: {
                self.selectionIndicatorView?.frame.origin.x = (gestureRecognizer.view?.frame.origin.x)!
                self.selectionIndicatorView?.frame = CGRect(x: (gestureRecognizer.view?.frame.origin.x)! + self.itemEdgeInsets.left, y: self.selectionIndicatorView!.frame.origin.y, width: (gestureRecognizer.view?.frame.width)! - self.itemEdgeInsets.left - self.itemEdgeInsets.right, height: self.selectionIndicatorHeight)
            })
            
            selectedView = gestureRecognizer.view
            
            target?.performSelector(action!, withObject: (gestureRecognizer.view as! UILabel).text)
        }
        
        
    }
    
    override func sizeThatFits(size: CGSize) -> CGSize {
        return fitSize
    }
    

    private var target: AnyObject?
    private var action: Selector?
    
    func addTarget(target: AnyObject, action: Selector) {
        self.target = target
        self.action = action
    }
    
    
    override func layoutSubviews() {
        
        
        
        var curX = CGFloat(0.0)
        
        
        
        for (index,itemView) in subviews.enumerate() {
            
            
            if !(itemView is UILabel) {
                continue
            }
            
            
                
            var calcWidth = CGFloat(0.0)
                
            if useSameWidth && widthOfItem > 0 {
                calcWidth = widthOfItem
            }
            else {
                    
                let fitSize  = itemView.sizeThatFits(CGSizeZero)
                    
                calcWidth = fitSize.width + itemEdgeInsets.left + itemEdgeInsets.right
            }
                
            if index == 0 {
                
                let size  = itemView.sizeThatFits(CGSizeZero)
                fitSize.height = size.height + itemEdgeInsets.top + itemEdgeInsets.bottom
            }
                
            itemView.frame = CGRect(x: curX, y: 0, width: calcWidth, height: frame.height)
                
            curX = curX + calcWidth
            
        }
        
        contentSize = CGSize(width: curX, height: frame.height)
        fitSize.width = curX
        
        if selectedView != nil {
            selectionIndicatorView?.frame = CGRect(x: selectedView!.frame.origin.x + itemEdgeInsets.left, y: frame.height  - selectionIndicatorHeight, width: (selectedView?.frame.width)! - itemEdgeInsets.left - itemEdgeInsets.right , height: selectionIndicatorHeight)
        }
        
    }
    
}
